package com.wocai.jrt.orgadmin.stats.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wocai.jrt.orgadmin.appointment.service.spi.AppointmentService;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.investor.service.spi.InvestorService;
import com.wocai.jrt.orgadmin.pay.service.spi.PaySerialService;
import com.wocai.jrt.orgadmin.product.model.Product;
import com.wocai.jrt.orgadmin.product.model.ProductBean;
import com.wocai.jrt.orgadmin.product.service.spi.ProductService;
import com.wocai.jrt.orgadmin.stats.bean.IndexStatsReqBean;
import com.wocai.jrt.orgadmin.stats.bean.TopFive;
import com.wocai.jrt.orgadmin.utils.MyDateUtils;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 投资人统计控制器
 * 
 * @author linbaolu
 * @date 2015年9月24日 16:33:40
 */
@Controller
@RequestMapping("/indexStats")
public class IndexStatsAction {
	private static final Logger LOGGER = LoggerFactory.getLogger(IndexStatsAction.class);
	@Inject
	private EmployeeService employeeServiceImpl;
	@Inject
	private InvestorService investorServiceImpl;
	@Inject
	private AppointmentService appointmentServiceImpl;
	@Inject
	private PaySerialService paySerialServiceImpl;
	@Inject
	private ProductService productServiceImpl;

	/**
	 * 跳转至数据统计首页
	 * 
	 * @param model
	 *            参数
	 * @return 首页路径 String
	 */
	@RequestMapping("/show")
	public String show(Map<String, Object> maps) {
		try {
			// 昨日新增投资人
			IndexStatsReqBean indexStatsReqBean = new IndexStatsReqBean();
			indexStatsReqBean.setDate(MyDateUtils.getYestoday());
			indexStatsReqBean.setOrgId(UserUtils.getUser().getOrgId());
			int investorYestodayCount = investorServiceImpl.yestodayCount(indexStatsReqBean);
			maps.put("investorYestodayCount", investorYestodayCount);

			// 昨日新增预约数
			int appointmentYestodayCount = appointmentServiceImpl.yestodayCount(indexStatsReqBean);
			maps.put("appointmentYestodayCount", appointmentYestodayCount);

			// 昨日销售额
			double paySerialYestodayAmount = paySerialServiceImpl.yestodayAmount(indexStatsReqBean);
			maps.put("paySerialYestodayAmount", Math.round(paySerialYestodayAmount / 10000));

			// 本月员工top5
			indexStatsReqBean.setDate(MyDateUtils.getThisMonth());
			List<TopFive> emTopFive = employeeServiceImpl.employeeTopFive(indexStatsReqBean);
			maps.put("emTopFive", emTopFive);

			// 正在募集的所有产品数
			ProductBean product = new ProductBean();
			product.createCriteria().andOrgIdEqualTo(UserUtils.getOrgId()).andStateEqualTo(1)
					.andAuditStateEqualTo(1);
			List<Product> list = productServiceImpl.queryForList(product);
			maps.put("size", list.size());

			// 本月员工组top5
			List<TopFive> emGroupTopFive = employeeServiceImpl
					.employeeGroupTopFive(indexStatsReqBean);
			maps.put("emGroupTopFive", emGroupTopFive);
			
			//统计将到期基金数量
			Product p = new Product();
			p.setOrgId(UserUtils.getOrgId());
			List<Product> products = productServiceImpl.pagedList(p);
			Integer SevenDuration = 0;
			Integer FoutyDuration = 0;
			Integer ThirtyDuration = 0;
			for (Product pro : products) {
				 Integer Duration = pro.getDuration(); 
				 Date payTime = pro.getSetupTime();//注意：此处需要改成产品的基金成立时间 之前是用的订单支付时间orders.getPayTime();
				 if(Duration != null && payTime !=null){
					 Date today = new Date();
					 Date maturityDate =DateUtils.addMonths(payTime,Duration);//基金到期日期
					 Integer i = MyDateUtils.countDays(today, maturityDate);
					 if(i>=0&&i<=7){
						 SevenDuration++;
					 }
					 if(i>=0&&i<=14){
						 FoutyDuration++;
					 }
					 if(i>=0&&i<=30){
						 ThirtyDuration++;
					 }
				 }
			}
			maps.put("SevenDuration", SevenDuration);
			maps.put("FoutyDuration", FoutyDuration);
			maps.put("ThirtyDuration", ThirtyDuration);
		} catch (Exception e) {
			LOGGER.error("show stats index page error, msg = [{}]", e.getMessage());
		}

		return "stats/index/indexStats";
	}

	// /**
	// * 测试缓存
	// */
	// @RequestMapping("/cache")
	// public void testCache() {
	// // 计算投资人统计数据
	// investorStatsServiceImpl.statsAllOrgInvestorInfo();
	// }

}
